<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Slowlyo\OwlAdmin\Models\BaseModel as Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

/**
 * 交换机管理
 */
class Switchs extends Model
{
	use SoftDeletes;

	protected $table = 'switch';
	protected static function boot()
	{
		parent::boot();

		static::creating(function ($model) {
			$model->created_at = date('Y-m-d H:i:s');
			$model->updated_at = date('Y-m-d H:i:s');
			$model->operator_uid = admin_user()->id;
			$model->editor_uid = admin_user()->id;
		});

		static::updating(function ($model) {
			$model->updated_at = date('Y-m-d H:i:s');
			$model->editor_uid = admin_user()->id;
		});
	}

	/**
	 * 将status字段映射为state以避免与amis关键字冲突
	 */
	protected $appends = ['state'];


	/**
	 * 获取state字段（status的别名）
	 */
	protected function getStateAttribute()
	{
		return $this->attributes['status'];
	}

	/**
	 * 重写toArray方法以隐藏status字段
	 */
	public function toArray()
	{
		$array = parent::toArray();
		
		// 移除status字段，但保留state作为别名
		unset($array['status']);
		
		return $array;
	}
}